/*
 * 作者: 侯晓宇
 * 时间: 2017-02-14
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	var SockJS = require('sockjs');
	require('stomp');
	
	var c = require('app/utils/CommonUtils');
	var HashMap = require('app/utils/HashMapUtils');
	var stop = true;
	var s = {};
	var stompClient = null;
	var win = window;
	
	win.mp = new HashMap();

	//需要现设置好路径
	s.subscribe = function(pattern, callback_func) {
		win.mp.put(pattern, callback_func);
	}

	//url为wssocket的endpoint
	s.connect = function(url, param, succ_callback_func, err_callback_func) {
	
		var socket = new SockJS(url);
		stompClient = Stomp.over(socket);
	
		stompClient.connect(param, function(frame) {
			var keySet = win.mp.keySet(); 
		    for(var i in keySet){
		    	var url =  keySet[i];
		    	var callback_func = win.mp.get(keySet[i]);  
		    	stompClient.subscribe(url,callback_func);
		    } 
		    
		    succ_callback_func();
	    },err_callback_func);
	};
	
	s.send = function(pattern, message){
		stompClient.send(pattern, {}, JSON.stringify(message));
	}

	s.disconnect = function() {
		if (stompClient != null) {
	        stompClient.disconnect();
	    }
	};


	return s;
});